articles

Home / DeveloperSection / Articles / Drag-Drop in TreeView in C#

Drag-Drop in TreeView in C#

AVADHESH PATEL 17704 20-Jul-2012

This article will briefly describe the basics of drag and drop in TreeView Control or Draggable TreeView Nodes in C#, the aim of the article is to describe how node dragging and swapping in TreeView, implemented in C#.

Steps: - Take a windows form and drag drop a TreeView Control and write code which is given below

Drag-Drop in TreeView in C#

Code:-

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace TreeViewDragDrop

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            TreeNode rootNode = treeView1.Nodes.Add("USA");

 

            // Create the Child nodes for the root

            TreeNode states1 = rootNode.Nodes.Add("New York");

            TreeNode states2 = rootNode.Nodes.Add("Michigan");

            TreeNode states3 = rootNode.Nodes.Add("Wisconsin");

            TreeNode states4 = rootNode.Nodes.Add("California");

 

            // Create siblings nodes for the Child nodes

            TreeNode child = states1.Nodes.Add("Rochester");

            child = states1.Nodes.Add("New York");

            child = states1.Nodes.Add("Albany");

            child = states2.Nodes.Add("Detroit");

            child = states2.Nodes.Add("Ann Arbor");

            child = states2.Nodes.Add("Lansing");

            child = states3.Nodes.Add("Milwaukee");

            child = states3.Nodes.Add("Madison");

            child = states3.Nodes.Add("La Cross");

            child = states4.Nodes.Add("Los Angeles");

            child = states4.Nodes.Add("San Fransisco");

            child = states4.Nodes.Add("San Diego");

 

            // Expand all tree nodes when first loaded

            treeView1.AllowDrop = true;

            treeView1.ExpandAll();

 

 

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

        }

 

        private void treeView1_DragEnter(object sender, DragEventArgs e)

        {

            e.Effect = DragDropEffects.Move;

        }

 

        private void treeView1_DragDrop(object sender, DragEventArgs e)

        {

            TreeNode SourceNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");

            Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));

            TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);

           

            try

            {

                int sourceIndex = SourceNode.Index;

                int destIndex = DestinationNode.Index;

 

                if (SourceNode.Level == 1 && DestinationNode.Level == 1)

                {

                    if (sourceIndex < destIndex)

                    {

                        TreeNode parentNode = SourceNode.Parent;

                        SourceNode.Remove();

                        DestinationNode.Remove();

                        parentNode.Nodes.Insert(sourceIndex, DestinationNode);

                        parentNode.Nodes.Insert(destIndex, SourceNode);

                    }

                    else

                    {

                        TreeNode parentNode = SourceNode.Parent;

                        SourceNode.Remove();

                        DestinationNode.Remove();

                        parentNode.Nodes.Insert(sourceIndex - 1, DestinationNode);

                        parentNode.Nodes.Insert(destIndex, SourceNode);

                    }

                }

 

                else if (SourceNode.Level == 2 && DestinationNode.Level == 2)

                {

                   

                    if (SourceNode.Parent == DestinationNode.Parent)

                    {

                        if (sourceIndex < destIndex)

                        {

                            TreeNode parentNode = SourceNode.Parent;

                            SourceNode.Remove();

                            DestinationNode.Remove();

                            parentNode.Nodes.Insert(sourceIndex, DestinationNode);

                            parentNode.Nodes.Insert(destIndex, SourceNode);

                        }

                        else

                        {

                            TreeNode parentNode = SourceNode.Parent;

                            SourceNode.Remove();

                            DestinationNode.Remove();

                            parentNode.Nodes.Insert(sourceIndex - 1, DestinationNode);

                            parentNode.Nodes.Insert(destIndex, SourceNode);

                        }

                    }

                    else

                    {

                        TreeNode addNode = DestinationNode.Parent;

                        SourceNode.Remove();

                        addNode.Nodes.Insert(destIndex, SourceNode);

                       

                    }

                }

 

                else if (SourceNode.Level == 2 && DestinationNode.Level == 1)

                { 

                    if (DestinationNode.Nodes.Count==0)

                    {

                        SourceNode.Remove();

                        DestinationNode.Nodes.Insert(0, SourceNode);               

                    }

                    else

                    {

                        int i = DestinationNode.LastNode.Index;

                        SourceNode.Remove();

                        DestinationNode.Nodes.Insert(i + 1, SourceNode);

                    }

                }

            }

            catch

            {

                MessageBox.Show("Drag on node...");

            }

        }

 

        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)

        {

            DoDragDrop(e.Item, DragDropEffects.Move);

        }

 

 

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

        {

 

        }

    }

}

 Output:-


Drag-Drop in TreeView in C#



Updated 11-Jul-2020
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Comments

Liked By